package com.museum.data;

import java.util.HashMap;
import java.util.Map;

import com.museum.model.Item_Info;
import com.museum.model.Location;
import com.museum.model.Mp_User_Bookshelf;
import com.museum.model.Open_Date;
import com.museum.model.Open_Date_Item;
import com.museum.model.Place;

public class Mp_User_Bookshelf_Handler {
	private Mp_User_Bookshelf mp_user_bookshelf;
	private Item_Info item_info;
	private Place exhibition_place;
	
	private String _username;
	private String _password;
	
	XmlRpc xmlrpc = new XmlRpc();
	public Mp_User_Bookshelf_Handler(String username,String password){
		_username = username;
		_password = password;
		mp_user_bookshelf = new Mp_User_Bookshelf();
		this.Parse();
		
	}
	public Mp_User_Bookshelf getMp_User_Bookshelf(){		
		return mp_user_bookshelf;
	}
	private void Parse(){		
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("username", _username);
		params.put("password", _password);
      	
      	Map<String, Object> response = xmlrpc.call("mp.user.bookshelf", params);
      	for (Map.Entry<String, Object> entry : response.entrySet()) {
            String key = entry.getKey();
            Object [] value = (Object[])entry.getValue();	
            for(int i = 0;i<value.length;i++){
            	ParseItemInfo(value[i]);
        	}
            //Log.d("TAG","key: " + key + ", value: " + value[0].toString());
      	}
	}
	private void ParseItemInfo(Object obj){
		item_info = new Item_Info();
        Map<String, Object> response3 = (Map<String, Object>)obj;
        for (Map.Entry<String, Object> entry3 : response3.entrySet()) {
        	String key3 = entry3.getKey();
            Object value3 = entry3.getValue();
            
            if(key3.equals("item_id")){
            	item_info.setItem_Id(value3.toString());
            }else if(key3.equals("display_name")){
            	item_info.setDisplay_Name(value3.toString());
            }else if(key3.equals("description")){
            	item_info.setDescription(value3.toString());
            }else if(key3.equals("keywords")){
            	item_info.setKeywords(value3.toString());
	      	}else if(key3.equals("dated_added")){
	      		item_info.setDated_Added(value3.toString());
	        }else if(key3.equals("related_to")){
	        	item_info.setRelate_To(value3.toString());
	        }else if(key3.equals("exhibition_place")){
	        	exhibition_place = new Place();
				Map<String, Object> response4 = (Map<String, Object>)value3;
		        for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
		        	String key4 = entry4.getKey();
		            Object value4 = entry4.getValue();
		            
		            if(key4.equals("place_id")){
		            	exhibition_place.setPlaceId(value4.toString());
		            }else if(key4.equals("email")){
		            	exhibition_place.setEmail(value4.toString());
		            }else if(key4.equals("website")){
		            	exhibition_place.setWebsite(value4.toString());
		            }else if(key4.equals("added_timestamp")){
		            	exhibition_place.setAdded_timestamp(value4.toString());
		            }else if(key4.equals("description")){
		            	exhibition_place.setDescription(value4.toString());
		            }else if(key4.equals("name")){
		            	exhibition_place.setName(value4.toString());
			      	}else if(key4.equals("create_timestamp")){
			      		exhibition_place.setCreate_timestamp(value4.toString());
			        }else if(key4.equals("exhibition_status")){
			        	exhibition_place.setExhibition_status(value4.toString());
			        }
			        else if(key4.equals("location")){
			        	Location location = new Location();
			            Map<String, Object> response5 = (Map<String, Object>)value4;
					    for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
					         String key5 = entry5.getKey();
						     Object value5 = entry5.getValue();								         
						        
						     if(key5.equals("longitude")){
						    	 location.setLongitude(value5.toString());
						     }else if(key5.equals("description")){
						        location.setDescription(value5.toString());
						     }else if(key5.equals("latitude")){
						        location.setLatitude(value5.toString());
						     }								        
					    }//End Loop4
					    exhibition_place.setLocation(location);
			        }else if(key4.equals("open_date")){
			        	Open_Date opendate = new Open_Date();
			        	Map<String, Object> response5 = (Map<String, Object>)value4;
					        for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
					           	String key5 = entry5.getKey();
						        Object value5 = entry5.getValue();
						        
						        Open_Date_Item opendateitem = new Open_Date_Item();
						        opendateitem.setDate(key5);
							    Map<String, Object> response6 = (Map<String, Object>)value5;
								for (Map.Entry<String, Object> entry6 : response6.entrySet()) {
									String key6 = entry6.getKey();
									Object value6 = entry6.getValue();									        
										        
									if(key6.equals("close_time")){
										opendateitem.setClose_time(value6.toString());
									}else if(key6.equals("open_time")){
										opendateitem.setOpen_time(value6.toString());
									}							
								}//End Loop5
								opendate.addItem(opendateitem);
								exhibition_place.setOpen_date(opendate);
					        }
			          }//End if			          
		        }
		        item_info.setExhibition(exhibition_place);
		        mp_user_bookshelf.addItem(item_info);
              }   
        }
	}
}
